package com.crm.CLdriving.service.impl;


import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.plugins.Page;
import com.crm.CLdriving.common.PageResponse;
import com.crm.CLdriving.dto.PageReq.PageNumber;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.req.CostDeleteReqDto;
import com.crm.CLdriving.dto.req.CostInsertReqDto;
import com.crm.CLdriving.dto.req.CostSelectOneReqDto;
import com.crm.CLdriving.dto.req.CostSelectXQReqDto;
import com.crm.CLdriving.dto.req.CostUpdateReqDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.CostMapper;
import com.crm.CLdriving.po.CostPO;
import com.crm.CLdriving.service.CostService;
import com.crm.CLdriving.util.DateUtils;

import lombok.extern.log4j.Log4j2;

@Transactional
@Service
@Log4j2
public class CostServiceImpl implements CostService {

	@Autowired
	private CostMapper costMapper;
	

	/**
	 * 缴费信息录入
	 * 根据身份证号identity判断数据是否存在
	 * 存在则不用录入
	 */
	@Override
	public BaseResponse<?> insert(CostInsertReqDto costInsertReqDto) {
		//自定义方法（jisuan）计算总费用
		Double zonfee = INSjisuan(costInsertReqDto);
		
		costInsertReqDto.setSumFee(zonfee);
		EntityWrapper<CostPO> wrapper = new EntityWrapper<>();
		wrapper.eq("identity", costInsertReqDto.getIdentity());
		List<CostPO> list = costMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			return BaseResponse.message(ResponseEnum.YJLR);
		}
		CostPO costPO = new CostPO();
		try {
			BeanUtils.copyProperties(costPO, costInsertReqDto);
			log.info("coyp后--"+costPO.toString());
		} catch (Exception e) {
			log.info("coyp---------"+e);
		}
		Integer row = costMapper.insert(costPO);
		if (row == 1) {
			log.info("录入缴费信息成功 row："+row);
			return BaseResponse.message(ResponseEnum.LRCG);
		}		
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	@Value("${data.pageSize.cost}")
	private int costSize;
	/**
	 * 查询（所有学员）缴费信息
	 */
	@Override
	public PageResponse<List<CostPO>> selectall(PageNumber pageNumber) {
		Page<CostPO> page = new Page<>(pageNumber.getPageNumber(),costSize);
		List<CostPO> list = costMapper.selectPage(page,null);
		//List<CostPO> list = costMapper.selectList(null);
		if (!"[]".equals(list.toString())) {
			log.info("查询到所有学员 缴费信息："+list.toString());
			return PageResponse.successOfPage(list,page);
		}
		return PageResponse.message(ResponseEnum.ZWSJ);
	}
	
	/**
	 * 更新缴费信息
	 * 通过id更新
	 */
	@Override
	public BaseResponse<?> update(CostUpdateReqDto costUpdateReqDto) {
		//自定义方法（jisuan）计算总费用
		Double zonfee = UPDjisuan(costUpdateReqDto);
		costUpdateReqDto.setSumFee(zonfee);
		costUpdateReqDto.setUpdateTime(DateUtils.format(new Date()));
		CostPO costPO = new CostPO();
		try {
			BeanUtils.copyProperties(costPO, costUpdateReqDto);
			log.info("copy后"+costPO.toString());
		} catch (Exception e) {
			log.info("coyp错误"+e);
		}		
		Integer row = costMapper.updateById(costPO);
		if (row == 1) {
			log.info("更新成功");
			return BaseResponse.message(ResponseEnum.GXCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}
	
	/**
	 * 根据id删除
	 */
	@Override
	public BaseResponse<?> deleteById(CostDeleteReqDto costDeleteReqDto) {
		Integer row = costMapper.deleteById(costDeleteReqDto.getId());
		if (row == 1) {
			return BaseResponse.message(ResponseEnum.SQCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}
	
	/**
	 * 通过Id
	 * 查询一条缴费信息
	 */
	@Override
	public BaseResponse<CostPO> selectone(CostSelectOneReqDto costSelectOneReqDto) {
		CostPO costPO = costMapper.selectById(costSelectOneReqDto.getId());
		if (costPO != null) {
			return BaseResponse.successOf(costPO);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}
	
	/**
	 * 搜索框查询
	 * 姓名 + 身份证
	 */
	@Override
	public BaseResponse<List<CostPO>> selectxq(CostSelectXQReqDto costSelectXQReqDto) {
		if(costSelectXQReqDto.getName()=="" && costSelectXQReqDto.getIdentityCard()==""){
			return BaseResponse.successOf(null);
		}
		EntityWrapper<CostPO> wrapper = new EntityWrapper<>();
		if (costSelectXQReqDto.getName() != "") {
			wrapper.like("name", costSelectXQReqDto.getName());
		}
		if (costSelectXQReqDto.getIdentityCard() != "") {
			wrapper.eq("identity", costSelectXQReqDto.getIdentityCard());
		}
		List<CostPO> list = costMapper.selectList(wrapper);	
		return BaseResponse.successOf(list);
	}
	
	
	
	
	/**
	 *  更新缴费信息 --计算总费用
	 */
	private Double UPDjisuan(CostUpdateReqDto costUpdateReqDto) {
		if (costUpdateReqDto.getDamageFee() == null) {
			costUpdateReqDto.setDamageFee(0.00);
		}
		if (costUpdateReqDto.getEnrollFee() == null) {
			costUpdateReqDto.setEnrollFee(0.00);
		}
		if (costUpdateReqDto.getMakeFee() == null) {
			costUpdateReqDto.setMakeFee(0.00);
		}
		if (costUpdateReqDto.getPracticeFee() == null) {
			costUpdateReqDto.setPracticeFee(0.00);
		}
		Double zonfee = costUpdateReqDto.getDamageFee()+costUpdateReqDto.getEnrollFee()+costUpdateReqDto.getMakeFee()+costUpdateReqDto.getPracticeFee();
		return zonfee;
	}

	/**
	 * 缴费信息录入 --计算总费用
	 */
	private Double INSjisuan(CostInsertReqDto costInsertReqDto) {	
		if (costInsertReqDto.getDamageFee() == null) {
			costInsertReqDto.setDamageFee(0.00);
		}
		if (costInsertReqDto.getEnrollFee() == null) {
			costInsertReqDto.setEnrollFee(0.00);
		}
		if (costInsertReqDto.getMakeFee() == null) {
			costInsertReqDto.setMakeFee(0.00);
		}
		if (costInsertReqDto.getPracticeFee() == null) {
			costInsertReqDto.setPracticeFee(0.00);
		}
		Double zonfee = costInsertReqDto.getDamageFee()+costInsertReqDto.getEnrollFee()+costInsertReqDto.getMakeFee()+costInsertReqDto.getPracticeFee();
		return zonfee;
	}

	

	

	

	

}
